package com.bankLogisticsSys.service;

import java.util.ArrayList;
import java.util.List;

import com.bankLogisticsSys.model.Expresspath;
import com.bankLogisticsSys.model.Networkinfo;
import com.jfinal.plugin.activerecord.Db;

public class PathService {
	public List<Networkinfo> getNetworkList(String type){
		if(type.equals("1")){//银行网点管理
			type = "(1)";
		}else if(type.equals("2")){//邮政网点管理
			type = "(2)";
		}else{
			type = "(3,4)";
		}
		List<Networkinfo> ni = Networkinfo.dao.find("select id, network from networkinfo where type in" + type);
		return ni;
	}
	
	public Boolean savePath(int networkID, int pathID, int sequence){
		Expresspath ep = new Expresspath();
		ep.setNetworkID(networkID);
		ep.setPathID(pathID);
		ep.setSequence(sequence);
		return ep.save();
	}
	
	public List<List<Object>> getPathList(){
		List<Integer> sourceList = Db.query("select distinct networkid from expresspath");
		List<List<Object>> pathList = new ArrayList<List<Object>>();
		for(int i=0, size=sourceList.size();i<size;i++){
			List<Object> temp = Db.query("select "
					+ "(select network from networkinfo where id = expresspath.pathid)network "
					+ "from expresspath where networkid = ?", sourceList.get(i));
			pathList.add(temp);
		}
		return pathList;
	}
	
	public boolean hasPath(int networkid){
		return (Expresspath.dao.find("select id from expresspath where networkid  = ?", networkid)!=null);
	}
	public boolean delPath(int networkid){
		List<Expresspath> epList = Expresspath.dao.find("select * from expresspath where networkid = ?", networkid);
		for(int i=0,size = epList.size();i<size;i++){
			epList.get(i).delete();
		}
		return true;
	}
}
